﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GeneticAlgorythm.Algorythm
{
    public class PermutationChromosome : Chromosome
    {

        private List<int> _genes;

        public PermutationChromosome(int size)
        {
            _genes = new List<int>();

            for (int i = 0; i < size; i++)
            {
                _genes.Add(-1);
            }
        }

        public PermutationChromosome(List<int> genes)
        {
            _genes = new List<int>();
            _genes.AddRange(genes);
        }

        public Object getGene(int i) {
            return  (Object) _genes[i];
        }
        public int getSize()
        {
            return _genes.Count;
        }
        public void setGene(int index, Object newGeneValue)
        {
            _genes.RemoveAt(index);
            _genes.Insert(index, (int) newGeneValue);
        }

        public int find(Object tmpGene)
        {
            for (int i = 0; i < getSize(); i++)
            {
                if (_genes[i] == (int) tmpGene) return i;
            }
            return -1;
        }

        public override bool Equals(object obj)
        {
            Chromosome otherChromosome = (Chromosome) obj;
            for (int i = 0; i < getSize(); i++)
           {
                if(!getGene(i).Equals(otherChromosome.getGene(i)))
                   return false;
            }

            return true;
        }

        public bool contains(Object obj)
        {
            return _genes.Contains((int) obj);
        }

        public override string ToString()
        {
            string result = "";
            foreach (int gene in _genes)
            {
                result += gene.ToString() + ",";
            }
            result += "\n";
            return result;
        }

    }

}
